﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Control;
using Enterprise.Model.Doc;
using Enterprise.Service.Doc;
using Enterprise.Model.Module;
using Enterprise.Service.Module;
namespace Enterprise.UI.Web.Modules.Doc
{
    public partial class Index : System.Web.UI.Page
    {
       protected int ModuleId = (int)Utility.sink("ModuleId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
       protected int ChildId = (int)Utility.sink("ChildId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
       string swhere = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            OnBindNav();
            //新增按钮
            CreateBT.AddButton("new.gif", "新增记录", "ArticlePub.aspx?ModuleId=" + ModuleId + "&ChildId=" + ChildId, Utility.PopedomType.List, HeadMenu1);
            if (!IsPostBack)
            {                
                BindGrid();
            }
        }

        private void OnBindNav()
        {
            string _nav = string.Format("<a href=\"Index.aspx?ModuleId="+ModuleId+"\">{0}</a><a style=\"color:black;\">&nbsp;  &nbsp;</a>", Enterprise.Service.Module.ModuleService.GetModuleName(ModuleId));
            ModuleService mService = new ModuleService();
            List<ModuleEntity> list = mService.ModuleList().Where(p => p.MPARENTID == ModuleId).OrderBy(p => p.MORDERBY).ToList();
            swhere = ModuleId.ToString();
            if (list.Count > 0)
            {
                int i = 0;                
                foreach (var var in list)
                {
                    i++;
                    swhere += "," + var.MODULEID;
                    if (i == list.Count)
                    {
                        _nav += string.Format("<a href=\"Index.aspx?ModuleId=" + ModuleId + "&ChildId="+var.MODULEID+"\">[ {0} ]</a>", var.MNAME);
                    }
                    else
                    {
                        _nav += string.Format("<a href=\"Index.aspx?ModuleId=" + ModuleId + "&ChildId=" + var.MODULEID + "\">[ {0} ]</a><a style=\"color:black;\">&nbsp; | &nbsp;</a>", var.MNAME);
                    }
                }
            }
            lb_Nav.Text = _nav;
        }

        private void BindGrid()
        {
            DocService docService = new DocService();
            List<DocArticleEntity> list = new List<DocArticleEntity>();
            if (ChildId == 0)
            {                
                string cmd = "select * from DOC_ARTICLES where CLASSID in (" + swhere + ") order by PUBDATE desc";
                list = docService.DocArticleList(cmd);
            }
            else
            {
                list = docService.DocArticleList(ChildId).OrderByDescending(p => p.PUBDATE).ToList();
            }

            //签收信息
            list = docService.DocQianshouList(list);

            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        public void Bt_Search_OnClick(object sender, EventArgs e)
        {
            string sType = (string)Utility.sink(s_Type.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            string sKey = (string)Utility.sink(s_Key.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            DocService docService = new DocService();
            //还需要限制在本栏目内的搜索
            var list = docService.DocArticleSearchList(sType, sKey);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

    }
}